using MicroMsg.Manager;
using MicroMsg.Storage;
using MicroMsg.UI.Page;
using MicroMsg.UI.Util;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
namespace MicroMsg.UI.UserContrl
{
	public class ModGroupMemberCtrl : UserControl
	{
		private const string TAG = "ModGroupMemberCtrl";
		public static DependencyProperty mMemberListProperty = DependencyProperty.Register("mMemberList", typeof(List<string>), typeof(ModGroupMemberCtrl), new PropertyMetadata(null, new PropertyChangedCallback(ModGroupMemberCtrl.OnmMemberListPropertyChanged)));
		public static DependencyProperty mBCanImportFromGroupProperty = DependencyProperty.Register("mBCanImportFromGroup", typeof(bool), typeof(ModGroupMemberCtrl), new PropertyMetadata(true));
		private bool mBIsFirstLoaded = true;
		private bool mBIsLoaded;
		private ModeGroupHeadCtrl mBtnAdd;
		private ModeGroupHeadCtrl mBtnDel;
		public bool mBIsAllCtrlTagShown;
		private List<Contact> mListCons = new List<Contact>();
		internal Paragraph paraPeoples;
		private bool _contentLoaded;
		public event MMStandardUIEvent mEventMemberChanged;
		public event MMStandardUIEvent mEventContactClicked;
		public List<string> mMemberList
		{
			get
			{
				return (List<string>)base.GetValue(ModGroupMemberCtrl.mMemberListProperty);
			}
			set
			{
				base.SetValue(ModGroupMemberCtrl.mMemberListProperty, value);
			}
		}
		public bool mBCanImportFromGroup
		{
			get
			{
				return (bool)base.GetValue(ModGroupMemberCtrl.mBCanImportFromGroupProperty);
			}
			set
			{
				base.SetValue(ModGroupMemberCtrl.mBCanImportFromGroupProperty, value);
			}
		}
		public ModGroupMemberCtrl()
		{
			this.InitializeComponent();
			base.add_Loaded(new RoutedEventHandler(this.Control_Loaded));
			base.add_Unloaded(new RoutedEventHandler(this.Control_Unloaded));
		}
		private static void OnmMemberListPropertyChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			if (dependencyPropertyChangedEventArgs_0.get_Property() == ModGroupMemberCtrl.mMemberListProperty)
			{
				(dependencyObject_0 as ModGroupMemberCtrl).mMemberListPropertyChanged(dependencyPropertyChangedEventArgs_0);
			}
		}
		private void mMemberListPropertyChanged(DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			if (dependencyPropertyChangedEventArgs_0.get_OldValue() != dependencyPropertyChangedEventArgs_0.get_NewValue())
			{
				this.RefreshItem();
			}
		}
		private void Control_Loaded(object sender, RoutedEventArgs e)
		{
			this.mBIsLoaded = true;
			Touch.add_FrameReported(new TouchFrameEventHandler(this.Touch_FrameReported));
		}
		private void Control_Unloaded(object sender, RoutedEventArgs e)
		{
			this.mBIsLoaded = false;
			Touch.remove_FrameReported(new TouchFrameEventHandler(this.Touch_FrameReported));
		}
		private void ctrl_mTagClick(object sender, object args)
		{
			if (sender == null)
			{
				return;
			}
			ModeGroupHeadCtrl modeGroupHeadCtrl = sender as ModeGroupHeadCtrl;
			if (modeGroupHeadCtrl == null)
			{
				return;
			}
			if (modeGroupHeadCtrl.mKind == ModeGroupHeadCtrlKind.Kind_Contact)
			{
				this.removeUser(modeGroupHeadCtrl.mContact);
				return;
			}
			if (modeGroupHeadCtrl.mKind == ModeGroupHeadCtrlKind.Kind_AddButton)
			{
				if (this.mBIsAllCtrlTagShown)
				{
					this.hideAllCtrlTag();
				}
				AddContactsPage.GotoThisPage(new AddContactsPageArgs
				{
					mDlgGetContacts = new MMStandardUIEvent(this.addConPageDlg),
					mType = ContactsViewType.CONTACT_VIEW_CHECKTOADD,
					mNFilterKind = 6,
					mListFilter = this.getCurNames(),
					mBCanImportFromGroup = this.mBCanImportFromGroup
				});
				return;
			}
			if (modeGroupHeadCtrl.mKind == ModeGroupHeadCtrlKind.Kind_DelButton)
			{
				if (this.mBIsAllCtrlTagShown)
				{
					this.hideAllCtrlTag();
					return;
				}
				this.showAllCtrlTag();
			}
		}
		private void addConPageDlg(object obj1, object obj2)
		{
			if (obj2 == null)
			{
				return;
			}
			List<Contact> listCons = obj2 as List<Contact>;
			this.addList(listCons);
		}
		private void ctrl_mItemClick(object sender, object args)
		{
			if (sender == null)
			{
				return;
			}
			ModeGroupHeadCtrl modeGroupHeadCtrl = sender as ModeGroupHeadCtrl;
			if (modeGroupHeadCtrl == null)
			{
				return;
			}
			if (modeGroupHeadCtrl.mKind == ModeGroupHeadCtrlKind.Kind_Contact)
			{
				if (this.mEventContactClicked != null)
				{
					this.mEventContactClicked(modeGroupHeadCtrl, modeGroupHeadCtrl.mContact);
					return;
				}
			}
			else
			{
				if (modeGroupHeadCtrl.mKind == ModeGroupHeadCtrlKind.Kind_AddButton)
				{
					if (this.mBIsAllCtrlTagShown)
					{
						this.hideAllCtrlTag();
					}
					AddContactsPage.GotoThisPage(new AddContactsPageArgs
					{
						mDlgGetContacts = new MMStandardUIEvent(this.addConPageDlg),
						mType = ContactsViewType.CONTACT_VIEW_CHECKTOADD,
						mNFilterKind = 6,
						mListFilter = this.getCurNames()
					});
					return;
				}
				if (modeGroupHeadCtrl.mKind == ModeGroupHeadCtrlKind.Kind_DelButton)
				{
					if (this.mBIsAllCtrlTagShown)
					{
						this.hideAllCtrlTag();
						return;
					}
					this.showAllCtrlTag();
				}
			}
		}
		private void Touch_FrameReported(object sender, TouchFrameEventArgs e)
		{
			if (this.mBIsAllCtrlTagShown && this.mBIsLoaded && this.mBtnDel != null)
			{
				try
				{
					bool flag = true;
					TouchPoint primaryTouchPoint = e.GetPrimaryTouchPoint(this);
					if (primaryTouchPoint.get_Action() == 1)
					{
						TouchPoint primaryTouchPoint2 = e.GetPrimaryTouchPoint(this.mBtnDel);
						if (primaryTouchPoint.get_Position().get_Y() > 0.0 && primaryTouchPoint.get_Position().get_X() > 0.0)
						{
							if (primaryTouchPoint.get_Position().get_Y() > base.get_ActualHeight())
							{
								flag = false;
							}
							else
							{
								if (primaryTouchPoint2.get_Position().get_Y() > 0.0 && primaryTouchPoint2.get_Position().get_X() >= this.mBtnDel.get_ActualWidth())
								{
									flag = false;
								}
							}
						}
						else
						{
							flag = false;
						}
						if (!flag)
						{
							this.hideAllCtrlTag();
						}
					}
				}
				catch
				{
				}
				return;
			}
		}
		private void RefreshItem()
		{
			this.setAddDelBtn();
			this.addList(this.mMemberList);
		}
		private void setAddDelBtn()
		{
			InlineUIContainer inlineUIContainer = new InlineUIContainer();
			this.mBtnAdd = this.getHeadCtrl();
			this.mBtnAdd.mKind = ModeGroupHeadCtrlKind.Kind_AddButton;
			this.mBtnAdd.SetImgTagSrc(ToolFunction.GetImgFromContentSource(R.theme.SnsGMemAdd));
			inlineUIContainer.set_Child(this.mBtnAdd);
			this.mBtnAdd.ShowTag();
			this.paraPeoples.get_Inlines().Add(inlineUIContainer);
			inlineUIContainer = new InlineUIContainer();
			this.mBtnDel = this.getHeadCtrl();
			this.mBtnDel.mKind = ModeGroupHeadCtrlKind.Kind_DelButton;
			this.mBtnDel.SetImgTagSrc(ToolFunction.GetImgFromContentSource(R.theme.SnsGMem_Del));
			inlineUIContainer.set_Child(this.mBtnDel);
			this.mBtnDel.ShowTag();
			this.mBtnDel.set_Visibility(1);
			this.paraPeoples.get_Inlines().Add(inlineUIContainer);
		}
		private void showDelButton()
		{
			if (this.mBtnDel != null)
			{
				this.mBtnDel.set_Visibility(0);
			}
		}
		private void hideDelButton()
		{
			if (this.mBtnDel != null)
			{
				this.mBtnDel.set_Visibility(1);
			}
		}
		private void showAllCtrlTag()
		{
			this.mBIsAllCtrlTagShown = true;
			List<ModeGroupHeadCtrl> allheadCtrls = this.getAllheadCtrls();
			using (List<ModeGroupHeadCtrl>.Enumerator enumerator = allheadCtrls.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					ModeGroupHeadCtrl current = enumerator.get_Current();
					if (current != null && current.mKind == ModeGroupHeadCtrlKind.Kind_Contact)
					{
						current.ShowTag();
					}
				}
			}
		}
		public void hideAllCtrlTag()
		{
			this.mBIsAllCtrlTagShown = false;
			List<ModeGroupHeadCtrl> allheadCtrls = this.getAllheadCtrls();
			using (List<ModeGroupHeadCtrl>.Enumerator enumerator = allheadCtrls.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					ModeGroupHeadCtrl current = enumerator.get_Current();
					if (current != null && current.mKind == ModeGroupHeadCtrlKind.Kind_Contact)
					{
						current.HideTag();
					}
				}
			}
		}
		private void addList(List<string> listUsers)
		{
			if (listUsers == null)
			{
				return;
			}
			using (List<string>.Enumerator enumerator = listUsers.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					string current = enumerator.get_Current();
					if (!string.IsNullOrEmpty(current) && -1 == this.findContactInCurlist(current))
					{
						Contact contact = StorageMgr.contact.method_0(current);
						if (contact == null)
						{
							contact = new Contact
							{
								strUsrName = current,
								nImgFlag = 4u
							};
						}
						this.mListCons.Add(contact);
						this.addOneCtrl(contact, 2);
					}
				}
			}
			if (this.mEventMemberChanged != null)
			{
				this.mEventMemberChanged(this, null);
			}
			if (this.mListCons.get_Count() != 0)
			{
				this.showDelButton();
			}
		}
		private void addList(List<Contact> listCons)
		{
			if (listCons == null)
			{
				return;
			}
			using (List<Contact>.Enumerator enumerator = listCons.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					Contact current = enumerator.get_Current();
					if (current != null && !string.IsNullOrEmpty(current.strUsrName) && -1 == this.findContactInCurlist(current))
					{
						this.mListCons.Add(current);
						this.addOneCtrl(current, 2);
					}
				}
			}
			if (this.mEventMemberChanged != null)
			{
				this.mEventMemberChanged(this, null);
			}
			if (this.mListCons.get_Count() != 0)
			{
				this.showDelButton();
			}
		}
		private int findContactInCurlist(Contact contact_0)
		{
			if (contact_0 == null)
			{
				return -1;
			}
			return this.findContactInCurlist(contact_0.strUsrName);
		}
		private int findContactInCurlist(string strUser)
		{
			if (string.IsNullOrEmpty(strUser))
			{
				return -1;
			}
			int num = 0;
			using (List<Contact>.Enumerator enumerator = this.mListCons.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					Contact current = enumerator.get_Current();
					if (current != null && !(current.strUsrName != strUser))
					{
						return num;
					}
					num++;
				}
			}
			return -1;
		}
		private void removeUser(string strName)
		{
			int nIndex = this.findContactInCurlist(strName);
			this.removeUserAt(nIndex);
		}
		private void removeUser(Contact contact_0)
		{
			int nIndex = this.findContactInCurlist(contact_0);
			this.removeUserAt(nIndex);
		}
		private void removeUserAt(int nIndex)
		{
			if (nIndex >= 0 && nIndex < this.mListCons.get_Count())
			{
				this.mListCons.RemoveAt(nIndex);
				this.removeCtrlAt(nIndex);
				if (this.mEventMemberChanged != null)
				{
					this.mEventMemberChanged(this, null);
				}
				if (this.mListCons.get_Count() == 0)
				{
					this.hideDelButton();
				}
				return;
			}
		}
		private List<string> getCurNames()
		{
			List<string> list = new List<string>();
			using (List<Contact>.Enumerator enumerator = this.mListCons.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					Contact current = enumerator.get_Current();
					if (current != null)
					{
						list.Add(current.strUsrName);
					}
				}
			}
			return list;
		}
		private void removeCtrlAt(int nIndex)
		{
			if (nIndex < 0)
			{
				return;
			}
			this.paraPeoples.get_Inlines().RemoveAt(nIndex);
		}
		private void addOneCtrl(Contact contact_0, int nIndexFromLas = 2)
		{
			if (nIndexFromLas > this.paraPeoples.get_Inlines().get_Count())
			{
				nIndexFromLas = 0;
			}
			InlineUIContainer inlineUIContainer = new InlineUIContainer();
			ModeGroupHeadCtrl headCtrl = this.getHeadCtrl();
			headCtrl.SetImgTagSrc(ToolFunction.GetImgFromContentSource("/Source/Plugin/Sns/Common/Resource/SnsGroup/SnsGMem_DelFrame.png"));
			headCtrl.mContact = contact_0;
			inlineUIContainer.set_Child(headCtrl);
			this.paraPeoples.get_Inlines().Insert(this.paraPeoples.get_Inlines().get_Count() - nIndexFromLas, inlineUIContainer);
		}
		private ModeGroupHeadCtrl getHeadCtrl()
		{
			ModeGroupHeadCtrl onItem = ModeGroupHeadCtrl.GetOnItem();
			onItem.set_Height(100.0);
			onItem.set_Width(100.0);
			onItem.set_Margin(new Thickness(0.0, 0.0, 13.0, 13.0));
			onItem.mItemClick += new MMStandardUIEvent(this.ctrl_mItemClick);
			onItem.mTagClick += new MMStandardUIEvent(this.ctrl_mTagClick);
			return onItem;
		}
		private List<ModeGroupHeadCtrl> getAllheadCtrls()
		{
			List<ModeGroupHeadCtrl> list = new List<ModeGroupHeadCtrl>();
			using (IEnumerator<Inline> enumerator = this.paraPeoples.get_Inlines().GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					Inline current = enumerator.get_Current();
					if (current != null)
					{
						InlineUIContainer inlineUIContainer = current as InlineUIContainer;
						if (inlineUIContainer != null && inlineUIContainer.get_Child() != null)
						{
							ModeGroupHeadCtrl modeGroupHeadCtrl = inlineUIContainer.get_Child() as ModeGroupHeadCtrl;
							if (modeGroupHeadCtrl != null)
							{
								list.Add(modeGroupHeadCtrl);
							}
						}
					}
				}
			}
			return list;
		}
		public List<string> GetNameList()
		{
			List<string> list = new List<string>();
			using (List<Contact>.Enumerator enumerator = this.mListCons.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					Contact current = enumerator.get_Current();
					if (current != null)
					{
						list.Add(current.strUsrName);
					}
				}
			}
			return list;
		}
		public List<Contact> GetContact()
		{
			return this.mListCons;
		}
		public void InitMemberList(List<Contact> conList)
		{
			List<string> list = new List<string>();
			using (List<Contact>.Enumerator enumerator = conList.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					Contact current = enumerator.get_Current();
					string text = null;
					if (current != null)
					{
						text = current.strUsrName;
					}
					if (!string.IsNullOrEmpty(text) && !list.Contains(text))
					{
						list.Add(text);
					}
				}
			}
			this.mMemberList = list;
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/UI/Contact/ModGroupMemberCtrl.xaml", 2));
			this.paraPeoples = (Paragraph)base.FindName("paraPeoples");
		}
	}
}
